package top.dancun.easbaseinfo.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.dancun.common.utils.PageUtils;
import top.dancun.common.utils.R;
import top.dancun.easbaseinfo.entity.TeacherEntity;
import top.dancun.easbaseinfo.service.TeacherService;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author dancun
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController extends AbstractController{
    @Autowired
    private TeacherService teacherService;

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R getTeacher(@PathVariable String id){
        TeacherEntity teacher = teacherService.getById(id);
        return R.ok().put("data",teacher);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("easbaseinfo:teacher:list")
    public R list(@RequestBody Map<String, Object> params){
        Map<String,String> dataMap = new HashMap<>(3);

        //判断空和空字符串
        dataMap.put("tId",
                Objects.nonNull(params.get("tId")) && "".equals(params.get("tId").toString())
                        ?params.get("tId").toString():null);
        dataMap.put("name",
                Objects.nonNull(params.get("name")) && "".equals(params.get("name").toString())
                        ?params.get("name").toString():null);
        dataMap.put("deptName",
                Objects.nonNull(params.get("deptName")) && "".equals(params.get("deptName").toString())
                        ?params.get("deptName").toString():null);

        PageUtils page = teacherService.queryPage(params,dataMap);

        return R.ok().put("page", page);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("easbaseinfo:teacher:save")
    public R save(@RequestBody TeacherEntity teacher){
        teacherService.save(teacher);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("easbaseinfo:teacher:update")
    public R update(@RequestBody TeacherEntity teacher){
        teacherService.updateById(teacher);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("easbaseinfo:teacher:delete")
    public R delete(@RequestBody String[] tids){
        teacherService.removeByIds(Arrays.asList(tids));

        return R.ok();
    }
}
